package com.org.nanohttpd;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.org.nanohttpd.test.WebServer;

import com.org.nanohpoptd.R;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {
	private static final String tag = "litao";
	private WebServer mLocalServer;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		startLocalServer();
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		mLocalServer.stop();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	private void startLocalServer() {
		// mLocalServer = new LocalServer(DEFAULT_FILE_PATH, VIDEO_WIDTH,
		// VIDEO_HEIGHT, LocalServer.DEFAULT_SERVER_PORT);
		List<File> rootDirs = new ArrayList<File>();
		File file = new File("");
		rootDirs.add(Environment.getExternalStorageDirectory());
		mLocalServer = new WebServer(null, 18030, rootDirs, false);
		try {
			mLocalServer.start();
		} catch (IOException e) {
			e.printStackTrace();
		}
		Log.i(tag, "" + mLocalServer.isAlive());
		Log.i(tag, "" + mLocalServer.getListeningPort());
	}
}
